/////////////////////////////////////////////////////////////////////////////////////////////		
*								 TABLE A.11
/////////////////////////////////////////////////////////////////////////////////////////////	

set matsize 11000

use "${inputdir}/prospects_analysis_dataset_clean.dta", clear
	
	drop if interviews!=2	
	
	
	gen lfi_L1 = lfi1anc
	gen psi_L1 = psi_std1
	
	reshape long lfi psi_std , i(respid) j(time)
	
	keep if time==2	
		
	cap drop SFC
	gen SFC = ANYSFC
	* drop if SFC==0								// change sample size if you want to look only on treated. reduces effects/significance by about half.

local controls1 = "female reg_age age2 i.r1_HighGrade"	
		
	rename psi_std psi
		
	foreach v in psi lfi{								// create residualized versions of outcomes as in first stage of Shue
		
		reg `v' `controls1' i.commid  					// had been in treatment group? why?
		predict `v'_resids, residuals
		
		reg `v' `v'_L1 `controls1' i.commid  
		predict `v'_resids2, residuals			
		}			

	keep lfi psi sfcgroupid commid psi_resids* lfi_resids* respid
	
	bys commid: gen cid = _n
	
	local bcsize0 = 434							// set biggest community sample seize here  FULL SAMPLE: 434	, 218 if treatment only
	local bcsize1 = `bcsize0' + 1
	
	foreach k in 1 2 3 4 5 6 7 8 9{
		count if commid==`k'
		local cnum`k' = r(N)
		di "`cnum`k''"
		}
	
	local k = 2									// set up wide set of variables for the diad differences in psi, lfi, and residualized versions
	while `k' < `bcsize1'{						// diads are labeled 2-N
		gen dpsi`k' = .
		gen dlfi`k' = .	
		gen dpsi_resids`k' = .
		gen dlfi_resids`k' = .		
		gen dpsi_residanc`k' = .
		gen dlfi_residanc`k' = .				
		gen sg`k' = .							//	 indicator for whether this diad was, in reality, assigned to the same SFC group
		gen partner`k' = .
		local k = `k' + 1
		}
		
	levelsof commid, local(comms)
	
	foreach q in `comms'{						// observation by observation calculation of differences for each diad
												// Done with the earlier cid (within community id) first
	
		local k = 1
		while `k' < `bcsize1'{
		
			di "Community `q', Individual `k'"
		
			qui sum lfi if cid==`k' & commid==`q'
			local a = r(mean)
			qui sum psi if cid==`k' & commid==`q'
			local b = r(mean)	
			qui sum lfi_resids if cid==`k' & commid==`q'
			local a2 = r(mean)
			qui sum psi_resids if cid==`k' & commid==`q'
			local b2 = r(mean)	
			qui sum lfi_resids2 if cid==`k' & commid==`q'
			local a3 = r(mean)
			qui sum psi_resids2 if cid==`k' & commid==`q'
			local b3 = r(mean)					
			
			qui sum sfcgroupid if cid==`k' & commid==`q'
			local g1 = r(mean)
					
			local j = `k' + 1
			while `j' <= `bcsize0'{	
				qui sum respid if cid==`j' & commid==`q'						// grab partner's id number
				qui replace partner`j' = r(mean) if cid==`k' & commid==`q'		// assign it to partner 1
				
				qui sum lfi if cid==`j' & commid==`q'
				local c = r(mean)
				qui sum psi if cid==`j' & commid==`q'
				local d = r(mean)	
				qui sum lfi_resids if cid==`j' & commid==`q'
				local c2 = r(mean)
				qui sum psi_resids if cid==`j' & commid==`q'
				local d2 = r(mean)
				qui sum lfi_resids2 if cid==`j' & commid==`q'
				local c3 = r(mean)
				qui sum psi_resids2 if cid==`j' & commid==`q'
				local d3 = r(mean)				
				
				local x = abs(`a' - `c')
				local y = abs(`b' - `d')
				
				local x2 = abs(`a2' - `c2')
				local y2 = abs(`b2' - `d2')	
				
				local x3 = abs(`a3' - `c3')
				local y3 = abs(`b3' - `d3')					
				
				qui replace dlfi`j' = `x' if cid==`k' & commid==`q'
				qui replace dpsi`j' = `y' if cid==`k' & commid==`q' 			
				
				qui replace dlfi_resids`j' = `x2' if cid==`k' & commid==`q'
				qui replace dpsi_resids`j' = `y2' if cid==`k' & commid==`q' 	
				
				qui replace dlfi_residanc`j' = `x3' if cid==`k' & commid==`q'
				qui replace dpsi_residanc`j' = `y3' if cid==`k' & commid==`q' 				
				
				qui sum sfcgroupid if cid==`j' & commid==`q'
				local g2 = r(mean)				
				
				qui replace sg`j' = `g1' == `g2' if cid==`k' & commid==`q'
					
				local j = `j' + 1
				}
			
			local k = `k' + 1	
		}	
	}
	
	local k = 2										// set same group = 0 if in control group (sfcgroupid>30)
	while `k' < `bcsize1'{
		replace sg`k'=0 if sfcgroupid>30
		local k = `k' + 1
		}
	
	gen uid = _n
	
	reshape long dpsi dlfi dpsi_resids dlfi_resids dpsi_residanc dlfi_residanc sg partner, i(uid) j(aid)				// turn all wide diads into unique obs
	
	drop if dpsi==. & dlfi==.
	gen all = 1	
	
	reg dpsi sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'
	estadd ysumm
	est sto r1
	
	reg dlfi sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r2
	
	reg dpsi_resids sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r3
	
	reg dlfi_resids sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r4
	
	reg dpsi_residanc sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r5
	
	reg dlfi_residanc sg, robust
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r6
	
	ngreg dpsi_residanc sg, group(commid) i(respid partner) symmetric	
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r7

	ngreg dlfi_residanc sg, group(commid) i(respid partner) symmetric	
	mat b = e(b)
	local b1 = b[1,1]
	local b2 = b[1,2]
	estadd scalar d = - `b1' / `b2'	
	estadd ysumm	
	est sto r8	
	
		# delimit ;
			esttab r1 r3 r5 r7 r2 r4 r6 r8 using "${outputdir}/tables/Table_A11.tex", replace b(%10.3f) se(%10.3f) scalars(
			"ymean Mean Y" "d Distance Ratio" "r2 R2")	sfmt(%10.4f %10.4f  %10.4f) star(* 0.10 ** 0.05 *** 0.01) keep() 
			order() mlabels("PSI-Diff" "PSI-Diff Resids" "PSI-Anc" "PSI-Cl" "LFI-Diff" "LFI-Diff Resids" "LFI-Anc" "LFI-Cl") nogaps varlabels(sg "Same Group" _cons "Constant")
			align(cccccc) title(Shue (2013) Peer Effect Analysis Estimation) nonotes nomtitles;
		# delimit cr	
